package cn.edu.xhu.library_management.user.controller;

import cn.edu.xhu.library_management.common.entity.R;
import cn.edu.xhu.library_management.user.entity.LmUserStudent;
import cn.edu.xhu.library_management.user.entity.search.StudentSearch;
import cn.edu.xhu.library_management.user.service.LmUserStudentService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: MMJ
 * @Date: 2023 - 04 - 18
 * @Description: 学生用户控制类
 */
@Api(tags = "学生管理模块")
@ApiSort(1)
@RestController
@RequestMapping("/lm_user/user/student")
public class UserStudentController {
    @Autowired
    private LmUserStudentService userStudentService;
    @Autowired
    private BCryptPasswordEncoder encoder;

    @GetMapping("/search")
    @ApiOperation(value = "学生搜索", produces = "application/json")
    @ApiOperationSupport(order = 111)
    public R search(StudentSearch search){
        Page<LmUserStudent> students= userStudentService.search(search);
        return R.ok().data("page", students);
    }

    @GetMapping("/{stuId}")
    @ApiOperation(value = "通过学生ID查询学生", produces = "application/json")
    @ApiOperationSupport(order = 112)
    public R getStudentById(@PathVariable("stuId") String stuId){
        LmUserStudent student = userStudentService.getById(stuId);
        return R.ok().data("student", student);
    }

    @PostMapping
    @ApiOperation(value = "添加学生用户", produces = "application/json")
    @ApiOperationSupport(order = 121)
    public R addUserStudent(@RequestBody LmUserStudent userStudent){
        String account = userStudent.getStAccount();
        String initPwd = account.substring(account.length() - 6);
        // 加密 默认密码 后六位
        userStudent.setStPwd(encoder.encode(initPwd));
        System.out.println(userStudent);
        boolean flag = userStudentService.save(userStudent);
        return flag ? R.ok("添加用户成功") : R.error("添加用户失败");
    }

    @PutMapping
    @ApiOperation(value = "修改学生用户", produces = "application/json")
    @ApiOperationSupport(order = 131)
    public R updateUserStudent(@RequestBody LmUserStudent userStudent){
        boolean flag = userStudentService.updateById(userStudent);
        return flag ? R.ok("修改用户成功") : R.error("修改用户失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过Id删除用户", produces = "application/json")
    @ApiOperationSupport(order = 141)
    public R deleteUserStudent(@PathVariable("id") String id){
        boolean flag = userStudentService.removeById(id);
        return flag ? R.ok("删除用户成功") : R.error("删除用户失败");
    }

    @DeleteMapping
    @ApiOperation(value = "删除用户(ids)", produces = "application/json")
    @ApiOperationSupport(order = 142)
    public R deleteUserStudent(@RequestParam("stuIds") List<String> stuIds){
        boolean flag = userStudentService.removeBatchByIds(stuIds);
        return flag ? R.ok("批量删除用户成功") : R.error("批量删除用户失败");
    }
}
